// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-9-6

#include "stdafx.h"
#include "roomping.h"
#include "roompage.h"
#include "mainuiman.h"
#include <common/ui/edit.h>
#include "chatview.h"

using namespace JoyMessage;

//##ModelId=44C4B5E4029F
CJoyRoomPing::CJoyRoomPing(void)
{
    m_IP    = 0;
    m_times = 0;
}

//##ModelId=44C4B5E402A1
CJoyRoomPing::~CJoyRoomPing(void)
{
}

//##ModelId=44C4B5E40292
void CJoyRoomPing::DoPing()
{
    -- m_times;

    CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
    CString chatText;
    chatText.Format("\nping(%d) request.", m_times);
    roomPage->m_chatView->AddText(chatText, 0);

    m_delay = GetTickCount();

    MC_PingRequest pingMsg;
    pingMsg.m_IP = m_IP;
    roomPage->m_socket.SendJoyMsg(&pingMsg);
}

//##ModelId=44C4B5E402A4
bool CJoyRoomPing::TestPing()
{
    CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
    CString strText = roomPage->m_chatEdit->GetText();

    int nIP, nTimes;
    if(0==sscanf((PCSTR)strText, "ping %d %d", &nIP, &nTimes))
    {
        return false;
    }

    m_IP = nIP;
    m_times = nTimes;

    if(m_times > 0)
    {
        DoPing();
    }

    return true;
}

//##ModelId=44C4B5E402A5
void CJoyRoomPing::OnMsg_PingRequest(const JoyMessage::MS_PingRequest* msg)
{
    CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
    MC_PingResponse pingMsg;
    pingMsg.m_IP = msg->m_IP;
    roomPage->m_socket.SendJoyMsg(&pingMsg);
}

//##ModelId=44C4B5E402AF
void CJoyRoomPing::OnMsg_PingResponse(const JoyMessage::MS_PingResponse*)
{
    CJoyRoomPage* roomPage = JoyGetMainManager()->m_roomPage;
    DWORD delay = GetTickCount()-m_delay;
    CString chatText;
    chatText.Format("\nping(%d) response : %d.", m_times, delay);
    roomPage->m_chatView->AddText(chatText, 0);

    if(m_times > 0)
    {
        DoPing();
    }
}
